﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Drawing;

namespace VIP_Shop.Business_Logic
{
    class CapNhap
    {
        
        DataTable datatable = new DataTable();
        VIP_Shop.Access_Data.AccessData data;
        BindingSource binding;
        Form frm;

        public CapNhap(VIP_Shop.GUI.CapNhap frm) 
        {
            data = new Access_Data.AccessData();
            this.frm = frm;
        }
        public void Load()
        {
            DataTable table = data.get_Datatable1("select * from loaihang");
            ComboBox cb = (ComboBox)frm.Controls["comboBox1"];
            cb.DataSource = table;
            cb.DisplayMember = "tenloai";
            cb.ValueMember = "maloai";

            datatable= data.get_Datatable("select * from mathang");
            binding = new BindingSource();
            binding.DataSource = datatable;
            ((BindingNavigator)frm.Controls["bindingNavigator1"]).BindingSource = binding;
            DataGridView grid = (DataGridView)frm.Controls["dataGridView1"];
            grid.DataSource = binding;
            grid.Columns["mahang"].ToolTipText = "Ma Hang Khong The Chinh Sua";
            grid.Columns["tenhang"].SortMode = DataGridViewColumnSortMode.Automatic;

            frm.Controls["txtmahang"].DataBindings.Add("Text", binding, "mahang");
            frm.Controls["txttenhang"].DataBindings.Add("Text", binding, "tenhang");
            frm.Controls["txtmota"].DataBindings.Add("Text", binding, "mota");
            frm.Controls["txthinh"].DataBindings.Add("Text", binding, "hinh");
           // frm.Controls["pictureBox1"].DataBindings.Add("ImageLocation", binding, "hinh");
            cb.DataBindings.Add("SelectedValue", binding, "maloai");
            
    
        }
        public void loadImage(PictureBox pic, string image)
        {
            try
            {
                pic.Image = Image.FromFile(image);
            }
            catch (System.IO.FileNotFoundException)
            {
                pic.Image = null;
            }
            catch (System.ArgumentException)
            {
                pic.Image = null;
            }
        }

        public void Insert()
        {
            try
            {
                string mahang = frm.Controls["txtmahang"].Text;
                string tenhang = frm.Controls["txttenhang"].Text;
                string mota = frm.Controls["txtmota"].Text;
                string hinh = frm.Controls["txthinh"].Text;
                string maloai = ((ComboBox)frm.Controls["comboBox1"]).SelectedValue.ToString();
                data.Update("insert into mathang(mahang,tenhang,mota,hinh,maloai)" + "values('"+ mahang + "','" + tenhang + "','" +mota +"','"+hinh+"','"+ maloai + "')");

            }
            catch (SqlException err)
            {
                MessageBox.Show(err.Message);
            }
        }
        public void Update()
        {
            try
            {
                string mahang = frm.Controls["txtmahang"].Text;
                string tenhang = frm.Controls["txttenhang"].Text;
                string mota = frm.Controls["txtmota"].Text;
                 string hinh = frm.Controls["txthinh"].Text;
                string maloai = ((ComboBox)frm.Controls["comboBox1"]).SelectedValue.ToString();
                data.Update("update mathang set "
                            + "tenhang='" + tenhang + "',"
                            + "maloai='" + maloai + "',"
                            + "mota='"+mota+"',"
                            + "hinh='"+hinh+"'"
                            +"where mahang='" + mahang + "'");

            }
            catch (SqlException err)
            {
                MessageBox.Show(err.Message);
            }
        }
        public void Delete()
        {
            DialogResult n = MessageBox.Show("Bạn có chắc chắn muốn xóa không?", "Delete", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                      
            if(n==DialogResult.OK)
            {
                try
                {
                    string mahang = frm.Controls["txtmahang"].Text;
                    data.Update("delete from mathang where mahang='"+mahang+"'");
                }
                catch(SqlException err)
                {
                    MessageBox.Show(err.Message);
                }
            }
        }
        public void cellEndEdit()
        {
            try
            {
                binding.EndEdit();
                data.UpdateView(this.datatable);
            }
            catch (SqlException err)
            {
                MessageBox.Show(err.Message);
            }
            
        }
        public void RowsAdded()
        {
            try
            {
                binding.EndEdit();
                data.UpdateView(this.datatable);
            }
            catch (SqlException err)
            {
                MessageBox.Show(err.Message);
            }
        }
        public void RowsRemoved()
        {
            try
            {
                binding.EndEdit();
                data.UpdateView(this.datatable);
            }
            catch (SqlException err)
            {
                MessageBox.Show(err.Message);
            }
        }
        public void Count()
        {
            frm.Controls["label7"].Text = "So mat hang: " +
                data.GetOneData("select count(*) from mathang").ToString();
        }
    }

   
}
